package com.ruoyi.yljf.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.yljf.domain.dto.ReportDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.ReportMapper;
import com.ruoyi.yljf.domain.Report;
import com.ruoyi.yljf.service.IReportService;

import javax.annotation.Resource;

/**
 * 举报记录Service业务层处理
 * 
 * @author metaee
 * @date 2025-03-15
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {
    @Resource
    private ReportMapper reportMapper;

    @Resource
    private ISysDictDataService dictDataService;

    /**
     * 查询举报记录列表
     * 
     * @param report 举报记录
     * @return 举报记录
     */
    @Override
    public List<Report> selectReportList(Report report) {
    	LambdaQueryWrapper<Report> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(report.getUserId())) {
			queryWrapper.eq(Report::getUserId, report.getUserId());
		}
        if (StringUtils.isNotNull(report.getCommentId())) {
			queryWrapper.eq(Report::getCommentId, report.getCommentId());
		}
        if (StringUtils.isNotNull(report.getReportType())) {
			queryWrapper.eq(Report::getReportType, report.getReportType());
		}
        return reportMapper.selectList(queryWrapper);
    }

    @Override
    public String addReprot(ReportDTO reportDTO) {
        Report report = BeanUtil.copyProperties(reportDTO, Report.class);
        report.setUserId(SecurityUtils.getUserId());
        report.setCreateTime(DateUtils.getNowDate());
        boolean flag = false;
        Report report1 = reportMapper.selectOne(new LambdaQueryWrapper<>(Report.class)
                .eq(Report::getReportType, report.getReportType())
                .eq(Report::getUserId, report.getUserId())
                .eq(Report::getCommentId, report.getCommentId())
                .eq(Report::getReportReason, report.getReportReason())
        );
        if(report1!= null)
            flag = false;
        else{
            int insert = reportMapper.insert(report);
            flag = insert > 0;
        }
        return flag ? "举报成功" : "已经举报过了,请勿重新举报";
    }
}
